package com.bigbee.gyf.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.p6spy.engine.common.P6Util;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

/**
 * 自定义打印日志格式
 * @author Hao Binbin
 */
public class MySingleLineFormat implements MessageFormattingStrategy {

	@Override
	public String formatMessage(int connectionId, String now, long elapsed,
			String category, String prepared, String sql) {
		// 把多个连续的空格用1个空格代替
		Pattern p = Pattern.compile("\\s+");
		Matcher m = p.matcher(sql);
		
		// 只打印执行时长和sql内容
		StringBuffer ret = new StringBuffer(200);
		ret.append("执行时长: ").append(elapsed);
		ret.append(" 毫秒; SQL: ").append(m.replaceAll(" ")).append(";");
		
		return P6Util.singleLine(ret.toString());
	}
}
